package com.sas.web.panel.clients;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.sas.domain.Article;
import com.sas.domain.Client;
import com.sas.model.EntityModel;
import com.sas.repository.ClientRepository;
import com.sas.utils.MessageHelper;
import com.sas.web.panel.SearchClientPanel;
import com.sas.web.panel.articles.EnterAmountPanel;

public class ClientsShortListPanel extends Panel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1365630608831780829L;
	
	private ModalWindow modal1 = null;
	
	private IModel<Article> articleModel;
	
	private boolean sell = false;
	
	
	@SpringBean
	private ClientRepository clientRepo;

	private EnterAmountPanel e;

	public ClientsShortListPanel(String id, IModel<Article> selectedArticle) {
		super(id);
		this.articleModel = selectedArticle;

		SortableDataProvider<IModel<Client>> dp = new ClientSortableDataProvider();
		init(dp);
//		dp.setSort("name", true);
//		
//		final DataView<IModel<Client>> dataView = createDataView(dp);
//		
//		add(new Label("article", MessageHelper.articleToSell(articleModel)));
//		
//		add(new SearchClientPanel("searchClient", articleModel));
//		add(new OrderByBorder("orderByCuilCuit", "cuilCuit", dp) {
//			/**
//			 * 
//			 */
//			private static final long serialVersionUID = -70924678511489644L;
//
//			@Override
//            protected void onSortChanged()
//            {
//                dataView.setCurrentPage(0);
//            }
//		});
//		
//		add(new OrderByBorder("orderByName", "name", dp) {
//			/**
//			 * 
//			 */
//			private static final long serialVersionUID = 778193645759174572L;
//
//			@Override
//            protected void onSortChanged()
//            {
//                dataView.setCurrentPage(0);
//            }
//		});
//		
//		add(modal1 = new ModalWindow("modal"));
//        add(dataView);
//        add(new PagingNavigator("navigator", dataView));
//        e = new EnterAmountPanel("enterAmount", modal1, articleModel) {
//			/**
//			 * 
//			 */
//			private static final long serialVersionUID = 5987247461393735476L;
//
//			@Override
//        	public boolean isVisible() {
//        		return sell;
//        	}
//        };
//        add(e);
	}
	


	@SuppressWarnings("serial")
	public ClientsShortListPanel(String id, final List<IModel<Client>> searchList, IModel<Article> selectedArticle) {
		super(id);
		this.articleModel = selectedArticle;
		SortableDataProvider<IModel<Client>> dp = new SortableDataProvider<IModel<Client>>() {
			@Override
			public Iterator<? extends IModel<Client>> iterator(int first,
					int count) {
				return searchList.iterator();
			}

			@Override
			public IModel<IModel<Client>> model(final IModel<Client> object) {
				return new LoadableDetachableModel<IModel<Client>>() {


					@Override
					protected IModel<Client> load() {
						return object;
					}
				};
			}

			@Override
			public int size() {
				return searchList == null ? 0 : searchList.size();
			}
		};
		
		init(dp);
//		dp.setSort("name", true);
//		
//		final DataView<IModel<Client>> dataView = createDataView(dp);
//		
//		add(new Label("article", MessageHelper.articleToSell(articleModel)));
//		
//		add(new SearchClientPanel("searchClient", articleModel));
//		add(new OrderByBorder("orderByCuilCuit", "cuilCuit", dp) {
//
//			@Override
//            protected void onSortChanged()
//            {
//                dataView.setCurrentPage(0);
//            }
//		});
//		
//		add(new OrderByBorder("orderByName", "name", dp) {
//			@Override
//            protected void onSortChanged()
//            {
//                dataView.setCurrentPage(0);
//            }
//		});
//		
//		add(modal1 = new ModalWindow("modal"));
//        add(dataView);
//        add(new PagingNavigator("navigator", dataView));
//        e = new EnterAmountPanel("enterAmount", modal1, articleModel) {
//			/**
//			 * 
//			 */
//			private static final long serialVersionUID = 5987247461393735476L;
//
//			@Override
//        	public boolean isVisible() {
//        		return sell;
//        	}
//        };
//        add(e);
	}
	
	private void init(SortableDataProvider<IModel<Client>> dp) {
		dp.setSort("name", true);
		
		final DataView<IModel<Client>> dataView = createDataView(dp);
		
		add(new Label("article", MessageHelper.articleToSell(articleModel)));
		
		add(new SearchClientPanel("searchClient", articleModel));
		add(new OrderByBorder("orderByCuilCuit", "cuilCuit", dp) {

			@Override
            protected void onSortChanged()
            {
                dataView.setCurrentPage(0);
            }
		});
		
		add(new OrderByBorder("orderByName", "name", dp) {
			@Override
            protected void onSortChanged()
            {
                dataView.setCurrentPage(0);
            }
		});
		
		add(modal1 = new ModalWindow("modal"));
        add(dataView);
        add(new PagingNavigator("navigator", dataView));
        e = new EnterAmountPanel("enterAmount", modal1, articleModel) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 5987247461393735476L;

			@Override
        	public boolean isVisible() {
        		return sell;
        	}
        };
        add(e);
	}

	private DataView<IModel<Client>> createDataView(
			SortableDataProvider<IModel<Client>> dp) {
		
		DataView<IModel<Client>> ret = new DataView<IModel<Client>>("clientsList", dp) {
			/**
			 * 
			 */
			private static final long serialVersionUID = -2946061176344721741L;

			@Override
			protected void populateItem(final Item<IModel<Client>> item) {
				final Client client = item.getModelObject().getObject();
				final String name = client.getName();
				item.add(new Label("cuilCuit", client.getCuilCuit()));
				item.add(new Label("name", name));
				item.add(new AjaxLink<Void>("select") {
					/**
					 * 
					 */
					private static final long serialVersionUID = 6985799436512634390L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						sell = true;
						e.setClientModel(item.getModelObject());
						Label label = new Label("client", name);
						label.setOutputMarkupId(true);
						target.addComponent(label);				
						setResponsePage(target.getPage());
					}
	            });
			}
		};
		
		return ret;
	}

	protected class ClientSortableDataProvider extends SortableDataProvider<IModel<Client>> {

		/**
		 * 
		 */
		private static final long serialVersionUID = 7006854411369075200L;

		@Override
		public Iterator<? extends IModel<Client>> iterator(int first, int count) {
			SortParam sortParam = getSort();
			String property = getSort().getProperty();
			if ("lastBuy".equalsIgnoreCase(property)) {
				property = "";
			}
			List<Client> list = clientRepo.findByProperty(sortParam.getProperty(), sortParam.isAscending());
			
			List<IModel<Client>> listIModel = new ArrayList<IModel<Client>>();
			for (Client c: list) {
				listIModel.add(new EntityModel<Client>(Client.class, c));
			}
			
			return listIModel.iterator();
		}

		@Override
		public IModel<IModel<Client>> model(final IModel<Client> object) {
			return new LoadableDetachableModel<IModel<Client>>() {

				/**
				 * 
				 */
				private static final long serialVersionUID = 8168453460760873721L;

				@Override
				protected IModel<Client> load() {
					return object;
				}
			};
		}

		@Override
		public int size() {
			List<Client> list = clientRepo.findAll();
			return list == null ? 0 : list.size();
		}
		
	}
}

;